package com.mcc.app.carbonEmission.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.common.config.ProjectConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.carbonEmission.mapper.ReportUploadInfoMapper;
import com.mcc.app.carbonEmission.domain.ReportUploadInfo;
import com.mcc.app.carbonEmission.service.IReportUploadInfoService;

/**
 * 文件上传信息Service业务层处理
 *
 * @author gxy
 * @date 2025-07-08
 */
@Service
@Slf4j
public class ReportUploadInfoServiceImpl implements IReportUploadInfoService {

    @Autowired
    private ReportUploadInfoMapper reportUploadInfoMapper;

    /**
     * 查询文件上传信息
     *
     * @param id 文件上传信息ID
     * @return 文件上传信息
     */
    @Override
    public ReportUploadInfo selectReportUploadInfoById(Long id) {
        return reportUploadInfoMapper.selectById(id);
    }

    /**
     * 查询文件上传信息列表
     *
     * @param reportUploadInfo 文件上传信息
     * @return 文件上传信息
     */
    @Override
    public List<ReportUploadInfo> selectReportUploadInfoList(ReportUploadInfo reportUploadInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(reportUploadInfo)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getUpDate()), "up_date", reportUploadInfo.getUpDate());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getBelong()), "belong", reportUploadInfo.getBelong());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getItemInfo()), "item_info", reportUploadInfo.getItemInfo());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getFilePath()), "file_path", reportUploadInfo.getFilePath());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getFileNameReal()), "file_name_real", reportUploadInfo.getFileNameReal());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getFileNameShow()), "file_name_show", reportUploadInfo.getFileNameShow());
            queryWrapper.eq(ObjectUtil.isNotEmpty(reportUploadInfo.getFileSize()), "file_size", reportUploadInfo.getFileSize());
        }
        return reportUploadInfoMapper.selectList(queryWrapper);
    }

    /**
     * 新增文件上传信息
     *
     * @param reportUploadInfo 文件上传信息
     * @return 结果
     */
    @Override
    public int insertReportUploadInfo(ReportUploadInfo reportUploadInfo) {
        return reportUploadInfoMapper.insert(reportUploadInfo);
    }

    /**
     * 修改文件上传信息
     *
     * @param reportUploadInfo 文件上传信息
     * @return 结果
     */
    @Override
    public int updateReportUploadInfo(ReportUploadInfo reportUploadInfo) {
        return reportUploadInfoMapper.updateById(reportUploadInfo);
    }

    /**
     * 删除文件上传信息
     *
     * @param id 文件上传信息ID
     * @return 结果
     */
    @Override
    public int deleteReportUploadInfoById(Long id) {
        return reportUploadInfoMapper.deleteById(id);
    }

    /**
     * 批量删除文件上传信息
     *
     * @param ids 文件上传信息ID
     * @return 结果
     */
    @Override
    public int deleteReportUploadInfoByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        ArrayList<String> nameList = new ArrayList<>();
        for(int i =0; i<ids.length;i++){
            ReportUploadInfo info = selectReportUploadInfoById(ids[i]);
            String filePath = info.getFilePath();
            if(StrUtil.isNotEmpty(filePath)){
                String[] array = filePath.split("/");
                nameList.add(array[array.length - 1]);
            }
        }
        delExportHisData(nameList.toArray(new String[0]));
        return reportUploadInfoMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    int delExportHisData(String[] names){
        int res = 0;
        for (int i = 0; i < names.length;i++){
            String dir = "furnaceExport";
            // 路径
            String path = ProjectConfig.getUploadPath() + "/" + names[i];// + "/" + dir + "/" + names[i];
            File file = new File(path);
            if (file.exists()) {
                if(file.delete()){
                    res += 1;
                    log.info("删除文件成功："+ path);
                }else{
                    log.info("删除文件失败："+ path);
                }
            }
        }
        return res;
    }
}
